package com.mrd.gtimp.sup.action;

import com.mrd.constant.MsgConstants;
import com.mrd.exception.QueryException;
import com.mrd.framework.config.web.json.Result;
import com.mrd.framework.ui.Jqgrid;
import com.mrd.framework.ui.filter.SearchRule;
import com.mrd.framework.ui.filter.SearchRuleOption;
import com.mrd.gtimp.sup.entity.SupSalesMan;
import com.mrd.gtimp.sup.service.SupSalesManBiz;
import com.mrd.util.LoginUtil;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;


/**
 * <p> application name: gtimp <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author luob
 */

@Controller
@RequestMapping("/sup")
public class SupSalesManAction {
    private static final long serialVersionUID = 1L;
    @Resource
    private SupSalesManBiz supSalesManBiz;

    /**
     * public String show() {
     * try {
     * page = initPage(SupSalesManBiz.findEntityCount(filters));
     * Order order = new Order();
     * order.add(sidx, sord);
     * SupSalesMans = SupSalesManBiz
     * .findEntityByFilter(page.getCurrentPageStartNo(), page.getPageSize(), filters, order);
     * } catch (Exception e) {
     * e.printStackTrace();
     * }
     * return SUCCESS;
     * }
     **/

    /**
     * 默认访问入口
     */
    @RequestMapping("/supSalesMan-main")
    public String main() {
        return "/gtimp/sup/supSalesManList";
    }

    /**
     * 默认新增修改入口
     */
    @RequestMapping("/supSalesMan-manage")
    public String manage(String id, ModelMap map) {
        map.put("id", id);
        return "/gtimp/sup/supSalesManManage";
    }

    /**
     * n 新增或修改信息
     */
    //// TODO: 2016/8/15   @Permissions
//    @Permissions(permissionsId = RoleConstants.SUP_SALESMAN_SAVE)
    @RequestMapping("/supSalesMan-saveOrUpdate")
    @ResponseBody
    public Result saveOrUpdate(SupSalesMan supSalesMan) {
        Result msg;
        // if(checkRight(RoleConstants.SUP_SALESMAN_SAVE)) {
        try {
            if (StringUtils.isEmpty(supSalesMan.getID())) {  // 新增操作
                supSalesMan.setCOM_ID(LoginUtil.getLoginUser().getCompany().getId());
                supSalesMan.setCREATOR(LoginUtil.getLoginUser().getAccount());
                supSalesMan.setCTIME(new Date());
                supSalesMan.setID(null);
            } else {  // 修改操作
                supSalesMan.setMODIFIER(LoginUtil.getLoginUser().getAccount());
                supSalesMan.setMTIME(new Date());
                if (supSalesMan.getLinkedUser() != null && StringUtils.isEmpty(supSalesMan.getLinkedUser().getId())) {
                    supSalesMan.setLinkedUser(null);
                }
            }
            supSalesManBiz.saveOrupdateEntity(supSalesMan);
            msg = Result.newSuccessInstance(MsgConstants.SAVE_SUECCES);
        } catch (Exception e) {
            e.printStackTrace();
            msg = Result.newFailInstance(MsgConstants.SAVE_FAIL + e.getMessage());
        }
        return msg;
    }

    /**
     * 编辑信息
     */
    @RequestMapping("/supSalesMan-find")
    @ResponseBody
    public SupSalesMan find(SupSalesMan supSalesMan) {
        if (null != supSalesMan) {
            if (StringUtils.isNotEmpty(supSalesMan.getID())) {
                try {
                    supSalesMan = supSalesManBiz.findEntityById(supSalesMan.getID());
                    return supSalesMan;
                } catch (QueryException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 删除信息
     */ //// TODO: 2016/8/15   @Permissions
//    @Permissions(permissionsId = RoleConstants.SUP_SALESMAN_DEL)
    @RequestMapping("/supSalesMan-del")
    @ResponseBody
    public Result del(String id) {
        Result msg;
        // if(checkRight(RoleConstants.SUP_SALESMAN_DEL)) {
        try {
            String[] idArr = id.split(",");
            List<SupSalesMan> men = new ArrayList<>();
            for (String string : idArr) {
                SupSalesMan supSalesMan = new SupSalesMan();
                supSalesMan.setID(string);
                men.add(supSalesMan);
            }
            supSalesManBiz.delete(men);
            msg = Result.newSuccessInstance(MsgConstants.DEL_SUCCES);
        } catch (Exception e) {
            e.printStackTrace();
            msg = Result.newFailInstance(MsgConstants.DEL_FAIL + e.getMessage());
        }
        return msg;
    }

    /**
     * 查询
     */
    @RequestMapping("/supSalesMan-findList")
    @ResponseBody
    public Page<SupSalesMan> findList(SupSalesMan supSalesMan, Jqgrid jqgrid) {
        String comId = LoginUtil.getLoginUser().getCompany().getId();// get from user
        try {
            SearchRule rule = new SearchRule("COM_ID", SearchRuleOption.eq, comId);
            jqgrid.addRule(rule);
            Page<SupSalesMan> supSalesMans = supSalesManBiz.findByJqgrid(jqgrid);
            if (supSalesMans != null && supSalesMans.getSize() > 0) {
                return supSalesMans;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Resource
    public void setSupSalesManBiz(SupSalesManBiz supSalesManBiz) {
        this.supSalesManBiz = supSalesManBiz;
    }

}
